﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BlobTowerDefense.Game_Entities;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace BlobTowerDefense.Managers
{
    public enum WAVETYPE { WAVE1, WAVE2, WAVE3, WAVE4, WAVE5, WAVE11, WAVE12, WAVE13, WAVE14, WAVE15};

    public class WaveManager
    {
        public static WAVETYPE CurrentWaveType;

        public static Dictionary<WAVETYPE, Wave> DefaultWaves_Level1;
        /*
        public static Dictionary<WAVETYPE, Wave> DefaultWaves_Level1 = new Dictionary<WAVETYPE,Wave>()
        {
            { WAVETYPE.WAVE1, new Wave(WAVETYPE.WAVE1, 10,  ENEMYTYPE.ball1, 10, PathManager.DefaultPaths["world1"], 0) },
            { WAVETYPE.WAVE2, new Wave(WAVETYPE.WAVE2, 15, ENEMYTYPE.ball2, 10, PathManager.DefaultPaths["world1"], 5) },
            { WAVETYPE.WAVE3, new Wave(WAVETYPE.WAVE3, 20, ENEMYTYPE.cube, 10, PathManager.DefaultPaths["world1"], 10) },
            { WAVETYPE.WAVE4, new Wave(WAVETYPE.WAVE4, 25, ENEMYTYPE.ring, 8, PathManager.DefaultPaths["world1"], 20) },
            { WAVETYPE.WAVE5, new Wave(WAVETYPE.WAVE5, 30, ENEMYTYPE.spikedBall, 8, PathManager.DefaultPaths["world1"], 20) }
        };
        */

        public static Dictionary<WAVETYPE, Wave> DefaultWaves_Level2;

        /*
        public static Dictionary<WAVETYPE, Wave> DefaultWaves_Level2 = new Dictionary<WAVETYPE, Wave>()
        {
            { WAVETYPE.WAVE11, new Wave(WAVETYPE.WAVE11, 15,  ENEMYTYPE.ball1, 8, PathManager.DefaultPaths["world2"], 0) },
            { WAVETYPE.WAVE12, new Wave(WAVETYPE.WAVE12, 20, ENEMYTYPE.ball2, 8, PathManager.DefaultPaths["world2"], 5) },
            { WAVETYPE.WAVE13, new Wave(WAVETYPE.WAVE13, 25, ENEMYTYPE.cube, 8, PathManager.DefaultPaths["world2"], 10) },
            { WAVETYPE.WAVE14, new Wave(WAVETYPE.WAVE14, 30, ENEMYTYPE.ring, 6, PathManager.DefaultPaths["world2"], 20) },
            { WAVETYPE.WAVE15, new Wave(WAVETYPE.WAVE15, 35, ENEMYTYPE.spikedBall, 6, PathManager.DefaultPaths["world2"], 20) }
        };
        */

        public static Wave CurrentWave;

        public WaveManager()
        {
            DefaultWaves_Level1 = new Dictionary<WAVETYPE, Wave>();

            DefaultWaves_Level1.Add(WAVETYPE.WAVE1, new Wave(WAVETYPE.WAVE1, 10, ENEMYTYPE.ball1, 10, PathManager.DefaultPaths["world1"], 0)); 
            DefaultWaves_Level1.Add(WAVETYPE.WAVE2, new Wave(WAVETYPE.WAVE2, 15, ENEMYTYPE.ball2, 10, PathManager.DefaultPaths["world1"], 5));
            DefaultWaves_Level1.Add(WAVETYPE.WAVE3, new Wave(WAVETYPE.WAVE3, 20, ENEMYTYPE.cube, 10, PathManager.DefaultPaths["world1"], 10));
            DefaultWaves_Level1.Add(WAVETYPE.WAVE4, new Wave(WAVETYPE.WAVE4, 25, ENEMYTYPE.ring, 8, PathManager.DefaultPaths["world1"], 20));
            DefaultWaves_Level1.Add(WAVETYPE.WAVE5, new Wave(WAVETYPE.WAVE5, 30, ENEMYTYPE.spikedBall, 8, PathManager.DefaultPaths["world1"], 20));

            DefaultWaves_Level2 = new Dictionary<WAVETYPE, Wave>();

            DefaultWaves_Level2.Add(WAVETYPE.WAVE11, new Wave(WAVETYPE.WAVE11, 15, ENEMYTYPE.ball1, 8, PathManager.DefaultPaths["world2"], 0));
            DefaultWaves_Level2.Add(WAVETYPE.WAVE12, new Wave(WAVETYPE.WAVE12, 20, ENEMYTYPE.ball2, 8, PathManager.DefaultPaths["world2"], 5));
            DefaultWaves_Level2.Add(WAVETYPE.WAVE13, new Wave(WAVETYPE.WAVE13, 25, ENEMYTYPE.cube, 8, PathManager.DefaultPaths["world2"], 10));
            DefaultWaves_Level2.Add(WAVETYPE.WAVE14, new Wave(WAVETYPE.WAVE14, 30, ENEMYTYPE.ring, 6, PathManager.DefaultPaths["world2"], 20));
            DefaultWaves_Level2.Add(WAVETYPE.WAVE15, new Wave(WAVETYPE.WAVE15, 35, ENEMYTYPE.spikedBall, 6, PathManager.DefaultPaths["world2"], 20));

            CurrentWave = null;
        }

        public void Update()
        {
            CurrentWave.Update();
        }

        public void LaunchWave()
        {
            CurrentWave.Launch();
        }

        public void PauseWave()
        {
            CurrentWave.Pause();
        }

        public bool NextWave()
        {
            if (CurrentWave == null)
            {
                if (GamePage.WorldIndex == 0)
                {
                    CurrentWave = DefaultWaves_Level1[WAVETYPE.WAVE1];
                    CurrentWaveType = CurrentWave.WaveType;
                }
                else
                {
                    CurrentWave = DefaultWaves_Level2[WAVETYPE.WAVE11];
                    CurrentWaveType = CurrentWave.WaveType;                    
                }
                return true;
            }
            else
            {
                if (CurrentWaveType == WAVETYPE.WAVE5)
                {
                    CurrentWave = DefaultWaves_Level2[WAVETYPE.WAVE11];
                    CurrentWaveType = CurrentWave.WaveType;
                    return false;
                }
                else
                {
                    CurrentWaveType++;
                    if (GamePage.WorldIndex == 0)
                    {                        
                        CurrentWave = DefaultWaves_Level1[CurrentWaveType];
                    }
                    else if (GamePage.WorldIndex == 1)
                    {
                        CurrentWave = DefaultWaves_Level2[CurrentWaveType];
                    }
                    return true;
                }
            }
        }

        public bool WaveRunning()
        {
            if (CurrentWave == null)
            {
                return false;
            }
            else
            {
                return CurrentWave.WaveRunning();
            }
        }

        public bool WaveEnded()
        {
            if (CurrentWave == null)
            {
                return false;
            }
            else
            {
                return CurrentWave.WaveEnded();
            }
        }

        public float? TimeToLaunchNextEnemy()
        {
            return CurrentWave.TimeToLaunchNextEnemy();
        }

        public EnemyEntity GetNextEnemy()
        {
            EnemyEntity enemyToReturn;
            ENEMYTYPE? enemyToReturnType;
            if ((enemyToReturnType = CurrentWave.NextEnemy()) == null)
            {
                enemyToReturn = null;
            }
            else
            {
                EnemyStats enemyToReturnStats = EnemyManager.DefaultEnemies[(ENEMYTYPE) enemyToReturnType];
                enemyToReturnStats.Armor += CurrentWave.WaveDifficultyModifier;
                enemyToReturnStats.Hp += CurrentWave.WaveDifficultyModifier;

                enemyToReturn = new EnemyEntity(enemyToReturnStats, CurrentWave.Path);
            }
            return enemyToReturn;
        }
    }
}
